package touro.mancala.master;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

public class Server {
	private RequestManager requestManager;
	private ResponseManager responseManager;
	private SlaveManager slaveManager;
	
	public Server() {
		
		try {
			
			slaveManager = new SlaveManager();
			
			requestManager = new RequestManager(this);
			new Thread(requestManager).start();
			
			responseManager = new ResponseManager(this);
			new Thread(responseManager).start();
			
			ClientAcceptListener clientListener = new ClientAcceptListener(this);
			new Thread(clientListener).start();
			
			SlaveAcceptListener slaveListener = new SlaveAcceptListener(this);
			new Thread(slaveListener).start();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	public SlaveManager getSlaveManager() {
		return slaveManager;
	}
	
	public RequestManager getRequestManager() {
		return requestManager;
	}

	public ResponseManager getResponseManager() {
		return responseManager;
	}


//	public void sendSlaveBoard(String board){
//		//right now it writes every board to the same slave...
//		try {
//			OutputStream out = slaves.get(0).getOutputStream();
//			out.write(board.getBytes());
//			out.flush();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}			
//	}
//	
//	public void resolveScores(int...scores){
//		int max = 0;
//		int index = 0;
//		for(int i : scores){
//			if(i > max){
//				max = i;
//				//index = the index of the move that returned this score
//			}
//		}
//		
//		// should write out directly?
//		for(Socket s : clients){
//			try {
//				OutputStream out = s.getOutputStream();
//				out.write(String.valueOf(index).getBytes());
//				out.flush();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//		}
//	}
	
}
